let qiniuTokenUrl = 'ido/qiniu/token'

function imguploadApi(vm){
	let qiniuToken = (lists)=>{
		let imgName = lists[0].file.name;
		let suffix = imgName.replace(/.+\./, '').toLowerCase();
		console.log('fileSuffix:', suffix);
		return vm.$u.get(qiniuTokenUrl,{suffix});
	}
	let qiniuUpload = ({fileUrl,key,token},callback)=>{
		uni.uploadFile({
			url: 'https://up-z2.qiniup.com',
			filePath: fileUrl,
			name: 'file',
			formData: {
				key,
				token
			},
			success: uploadFileRes => {
				console.log('upres', uploadFileRes);
				callback(uploadFileRes)
				return uploadFileRes
			},
			fail:()=>{
				uni.showToast({
					title:'error',
					icon:'none',
					duration:1000
				})
			}
		});
	}
	let qiniu = async (lists,callback)=>{
		let imgName = lists[0].file.name;
		let Suffix = imgName.replace(/.+\./, '').toLowerCase();
		console.log('fileSuffix:',Suffix);
		let tokenRes = await vm.$u.get(qiniuTokenUrl,{Suffix});
		console.log('res', tokenRes);
		
		let token = tokenRes.token;
		let key = tokenRes.key;
		let fileUrl = lists[0].url;
		vm.$u.api.qiniuUpload({token,fileUrl,key},callback)
	}
	return {qiniuToken,qiniuUpload,qiniu}
}
export default imguploadApi